package de.sordul.goldwaage;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;

import de.sordul.goldwaage.core.NGramUtil;
import de.sordul.goldwaage.core.TextUtil;

/**
 * Singleton class providing the functionality of this library.
 * @author Damian Berning
 */
public class Goldwaage {

	private static Goldwaage goldwaage = null;

	/**
	 * Default constructor
	 */
	public Goldwaage() { }
	
	/**
	 * Factory method
	 * @return Goldwaage instance
	 */
	public static Goldwaage getInstance() {
		if(goldwaage == null)
			goldwaage = new Goldwaage();
		return goldwaage;
	}
	
	/**
	 * Calculate the difference between two strings in percent. It's important in which order
	 * the strings are given. The method compares how much of the first string can be found in
	 * the second one. An desired maximum ngram length of 5 is assumed.
	 * @param text1 The first string
	 * @param text2 The second string
	 * @return The difference in percent
	 */
	public float getDifference(String text1, String text2) {
		return NGramUtil.oopDiff(
				NGramUtil.generate(text1, 5), 
				NGramUtil.generate(text2, 5)
		);
	}
	
	
	/**
	 * Calculate the difference between two strings in percent. It's important in which order
	 * the strings are given. The method compares how much of the first string can be found in
	 * the second one.
	 * @param text1 The first string
	 * @param text2 The second string
	 * @param lengthOfNGrams Diesired maximum length of ngrams
	 * @return The difference in percent
	 */
	public float getDifference(String text1, String text2, int lengthOfNGrams) {
		return NGramUtil.oopDiff(
				NGramUtil.generate(text1, lengthOfNGrams), 
				NGramUtil.generate(text2, lengthOfNGrams)
		);
	}
	
	
	/**
	 * Calculate the difference between two ngrams in percent. It's important in which order
	 * the ngrams are given. The method compares how much of the ngrams in the first map can
	 * be found in the second one.
	 * @param text1 The first ngram
	 * @param text2 The second string
	 * @return The difference in percent
	 */
	public float getDifference(Set<String> ngrams1, Set<String> ngrams2) {
		return NGramUtil.oopDiff(
				ngrams1, 
				ngrams2
		);
	}
	
	
	/**
	 * Calculate the similarity between two strings in percent. It's important in which order
	 * the strings are given. The method compares how much of the first string can be found in
	 * the second one. An desired maximum ngram length of 5 is assumed.
	 * @param text1 The first string
	 * @param text2 The second string
	 * @return The similarity in percent
	 */
	public float getSimilarity(String text1, String text2) {
		float difference = NGramUtil.oopDiff(
				NGramUtil.generate(text1, 5), 
				NGramUtil.generate(text2, 5)
		);
		
		return (difference-100)*(-1);
	}
	
	
	/**
	 * Calculate the similarity between two strings in percent. It's important in which order
	 * the strings are given. The method compares how much of the first string can be found in
	 * the second one. An desired maximum ngram length of 5 is assumed.
	 * @param text1 The first string
	 * @param text2 The second string
	 * @param lengthOfNGrams Diesired maximum length of ngrams
	 * @return The similarity in percent
	 */
	public float getSimilarity(String text1, String text2, int lengthOfNGrams) {
		float difference = NGramUtil.oopDiff(
				NGramUtil.generate(text1, lengthOfNGrams), 
				NGramUtil.generate(text2, lengthOfNGrams)
		);
		
		return (difference-100)*(-1);
	}
	
	
	/**
	 * Calculate the similarity between two ngrams in percent. It's important in which order
	 * the ngrams are given. The method compares how much of the ngrams in the first map can
	 * be found in the second one.
	 * @param text1 The first ngram
	 * @param text2 The second string
	 * @return The similarity in percent
	 */
	public float getSimilarity(Set<String> ngrams1, Set<String> ngrams2) {
		float difference = NGramUtil.oopDiff(
				ngrams1, 
				ngrams2
		);
		
		return (difference-100)*(-1);
	}
	
	/**
	 * Extracts the sentences from a String and returns them as List<String>
	 * @param text The text
	 * @return The resulting List<String>
	 */
	public List<String> getSentences(String text) {
		return TextUtil.extractSentences(text);
	}

	/**
	 * Extracts the sentences from a textfile and returns them as List<String>
	 * @param text The textfile
	 * @return The resulting List<String>
	 * @throws IOException File not found
	 */
	public List<String> getSentences(File textFile) throws IOException {
		String text = FileUtils.readFileToString(textFile);
		return getSentences(text);
	}
	
	/**
	 * Guesses the language of a given string and returns the result as ISO 639-1 string
	 * @param string The string to guess the language of
	 * @return ISO 639-1 code for guessed language
	 */
	public String guessLanguage(String string) {
		return null;
	}
}
